//
//  LoginView.swift
//  01MicroBlog
//
//  Created by Apple on 15/7/29.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

// MARK: - 定义代理协议
protocol LoginViewDelegate: NSObjectProtocol {
    // 访客视图将要登录
    func loginViewWillLogin()
    // 访客视图将要注册
    func loginViewWillRegist()
}

class LoginView: UIView {

    // 定义代理属性
    weak var delegate:LoginViewDelegate?
    
    // 代码创建View的时候调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.whiteColor()
        // 设置子控件
        setUpSubViews()
    }
    
    // MARK: - 提供接口，设置登录页面的图片、文字
    func setUpInformation(isHome:Bool, imageName:String, introText:String) {
        
        iconView?.image = UIImage(named: imageName)
        introLabel?.text = introText
        if isHome {
            houseView?.hidden = false
            startRotation()
        } else {
            houseView?.hidden = true
            sendSubviewToBack(maskIconView!)
        }
    }
    
    // 重写init方法，保证用SB也能创建本类
    required init?(coder aDecoder: NSCoder) {
        
        // 禁止使用SB调用本类方法。使用SB创建View的时候，会奔溃
//        fatalError("init(coder:) has not been implemented")
        
        // 以下两句代码会保证SB创建View的时候，也能调用
        super.init(coder: aDecoder)
        setUpSubViews()
    }
    
    // MARK:设置子控件的方法
    func setUpSubViews() {
        // 将子控件加入到View
        addSubview(iconView!)
        addSubview(maskIconView!)
        addSubview(houseView!)
        addSubview(introLabel!)
        addSubview(registBtn!)
        addSubview(loginBtn!)
        
        // 设置子控件的约束
        // 设置自动约束前要设置一个属性
        
        
        // 给iconView设置约束
        iconView?.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -50))

        
        // 给houseView设置约束
        houseView?.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))

        // 给label设置约束
        introLabel?.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: introLabel!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: introLabel!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // 设置宽度
        addConstraint(NSLayoutConstraint(item: introLabel!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))

        // 给注册按钮设置约束
        registBtn!.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registBtn!, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registBtn!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 给登录按钮设置约束
        loginBtn!.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn!, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
//         给maskIconView设置约束
        maskIconView?.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: maskIconView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView!, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView!, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView!, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 30))
    
        
        self.backgroundColor = UIColor(red: 237.0/255.0, green: 237.0/255.0, blue: 237.0/255.0, alpha: 1.0)
        
    }
    
    // 开始动画
    func startRotation() {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = M_PI * 2
        ani.duration = 20
        ani.repeatCount = MAXFLOAT
        ani.removedOnCompletion = false
        
       iconView?.layer.addAnimation(ani, forKey: nil)
    }
    
    // mark:懒加载控件
    // 懒加载转动的图片框
    private lazy var iconView:UIImageView? = {
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return iconView
    }()
    
    // 懒加载遮罩
    private lazy var maskIconView:UIImageView? = {
       let maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return maskIconView
    }()
    
    // 懒加载房子
    private lazy var houseView:UIImageView? = {
        let houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return houseView
    }()
    
    // 懒加载介绍标签
    private lazy var introLabel:UILabel? = {
        let introLabel = UILabel()
        introLabel.numberOfLines = 0
        introLabel.text = "关注一些人，回这里看看有什么惊喜"
        introLabel.font = UIFont.systemFontOfSize(14)
        introLabel.sizeToFit()
        introLabel.textColor = UIColor.darkGrayColor()
        
        return introLabel
    }()
    
    // 点击了登录按钮
    func loginBtnClick() {
        delegate?.loginViewWillLogin()
    }
    
    // 点击了注册按钮
    func registBtnClick() {
        delegate?.loginViewWillRegist()
    }
    
    // 懒加载登录按钮
    private lazy var loginBtn:UIButton? = {
       let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 给界面上的登录按钮增加一个监听方法
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
    }()
    
    // 懒加载注册按钮
    private lazy var registBtn:UIButton? = {
       let registBtn = UIButton()
        registBtn.setTitle("注册", forState: UIControlState.Normal)
        registBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 给界面上的登录按钮增加一个监听方法
        registBtn.addTarget(self, action: "registBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registBtn
    }()

}


